﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace ImageK.Util
{
    public class FormUtil
    {
        [DllImport("user32.dll")]
        static extern IntPtr GetDC(IntPtr ptr);

        [DllImport("user32.dll", EntryPoint = "ReleaseDC")]
        static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDc);

        [DllImport("gdi32.dll", EntryPoint = "GetDeviceCaps", SetLastError = true)]
        public static extern int GetDeviceCaps(IntPtr hdc, int nIndex);

        public static double GetScreenScalingFactor()
        {
            IntPtr desktop = GetDC(IntPtr.Zero);
            var physicalScreenHeight = (float)GetDeviceCaps(desktop, (int)DeviceCap.DESKTOPVERTRES);
            var vertresScreenHeight = (float)GetDeviceCaps(desktop, (int)DeviceCap.VERTRES);

            var screenScalingFactor =
                (double)physicalScreenHeight / vertresScreenHeight;

            ReleaseDC(IntPtr.Zero, desktop);

            return screenScalingFactor;
        }
    }

    enum DeviceCap
    {
        VERTRES = 10,
        PHYSICALWIDTH = 110,
        SCALINGFACTORX = 114,
        DESKTOPVERTRES = 117,

        // http://pinvoke.net/default.aspx/gdi32/GetDeviceCaps.html
    }
}
